package com.jiangruanjavaproject.request;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;
import java.util.Set;

/**
 * 该函数用于实现get请求,并将请求的结果返回
 * @author:wanfeng
 * @desc:封装网络请求
 * @datetime:2021/12/9 16:36
 */
public class HttpUtils {
   public static String get(String path){
       InputStream inputStream =null;
       InputStreamReader inputStreamReader = null;
       BufferedReader reader =null;

       String line = "";//最开始空行
       String result = "";//每次读取到以后多要积累起来的读取
       try {
           URL url = new URL(path);
           URLConnection conn = url.openConnection();
           //进行连接
           conn.connect();
           //
           inputStream = conn.getInputStream();

           //字符流：按行读
           inputStreamReader = new InputStreamReader(inputStream);
           reader = new BufferedReader(inputStreamReader);
//            String line = reader.readLine();
//            while (line != null){
//                reader.readLine();
//            }
           while ((line = reader.readLine()) != null){
               result += line;
           }
       } catch (MalformedURLException e) {
           e.printStackTrace();
       }catch (IOException e) {
           e.printStackTrace();
       }finally{
           //关闭读取流

           try {
               if (reader != null) {
                   reader.close();
               }
               if (inputStreamReader != null){
                   inputStreamReader.close();
               }
               if (inputStream != null){
                   inputStream.close();
               }
           } catch (IOException e) {
               e.printStackTrace();
           }

       }


       return result;
   }

    /**
     * 封装java中的post原生请求,并返回请求结果
     * @param path 请求路径
     * @param header 请求的头设置
     * @param data 要求发送的请求的数据
     * @return 请求的结果
     */
   public static String post(String path, Map<String,String> header,byte[] data){
       OutputStream outputStream = null;
       InputStream  inputStream= null;
       BufferedReader reader = null;
       String result = "";//null{}

       try {

           URL url = new URL(path);
           HttpURLConnection conn =(HttpURLConnection) url.openConnection();
           conn.setRequestMethod("POST");//设置请求类型
           conn.setDoOutput(true);//设置可以向外发送数据
           conn.setDoInput(true);//设置可以读取数据
           //设置请求头信息
           //ContentType:application/json
           //Authorzation:basic +xxx
           //如何读取map数据
           //集合
           //List:有序,for循环,foreach

           //Set:无序,foreach,迭代器
           //Mao:无序,键值对,String value = get(key)

        if (!header.isEmpty()){
            Set<String>keys =header.keySet();
            for (String key:keys){
                String value = header.get(key);
                conn.setRequestProperty(key,value);
            }
           }
        //进行连接
           conn.connect();
        //将数据发送出去
           outputStream = conn.getOutputStream();
           outputStream.write(data);
           //接受响应数据
           inputStream = conn.getInputStream();
            //IO流
           //输入流: 字节输入流(InputStream)  字节读取流(Reader)

           //输出流:字节输出流(OutputStream) 字节写入流(Writer)
           //转换流:
           //InputStreamreader
           //OutputStreamWriter
           InputStreamReader inputStreamReader =  new InputStreamReader(inputStream);
          reader = new BufferedReader(inputStreamReader);
          String line = null;
          while((line = reader.readLine())!= null){
              result +=line;
          }
          //           inputStream.read()
       } catch (MalformedURLException e) {
           e.printStackTrace();
       } catch (IOException e) {
           e.printStackTrace();
       }finally{
           try{
               if (reader!=null){
                   reader.close();
               }
               if (inputStream!=null){
                  inputStream.close();
               }
               if (outputStream!=null){
                   outputStream.close();
               }
           } catch (Exception e) {
               e.printStackTrace();
           }
       }



return  result;
}
}

